package com.m3958.firstgwt.server.model;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import com.m3958.firstgwt.server.types.JsonExcludeFields;
import com.m3958.firstgwt.server.utils.JsonlibDateValueProssor;
import com.m3958.firstgwt.shared.type.CommonField;
import com.m3958.firstgwt.shared.type.VoteTplField;

@Entity
@Table(name="VOTE_TPL")
@AttributeOverride(name="id",column=@Column(nullable = false,name="VOTE_TPL_ID"))
public class VoteTpl extends BaseModel<VoteTpl> implements HasCreator{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	public JSONObject toJson(){
		JsonConfig jc = new JsonConfig();
		jc.registerJsonValueProcessor(Date.class, injector.getInstance(JsonlibDateValueProssor.class));
		jc.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		jc.setExcludes(JsonExcludeFields.VOTE_TPL_EXCLUDES);
		
		return JSONObject.fromObject(this, jc);
	}
	
	@Override
	public JSONObject toJson(String jsonv){
		if(jsonv != null && jsonv.length()>0){
			JSONObject jo = toJson();
			jo.element(CommonField.CONTENT.getValue(), getContent());
			return jo;
		}else{
			return toJson();
		}
	}
	
	protected String creatorIds;

	@ManyToOne
	@JoinColumn(name="CREATOR_ID",referencedColumnName="USER_ID")
	protected User creator;
	
	private String name;
	
	private boolean openSource;
	
	private String flag;//system:可以免费永久使用。public：可供选择（对于付费vote，永久可用，对于免费vote，限制次数）

	private String lang;
	
	private String type;//简单，二维等等
	
	private String dspType;//embeded,wholepage
	
	@Lob
	private String tplUsage;
	
	@Lob
	private String outScript;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date updatedAt;
	
	@Lob
	private String content;
	
	private String previewUrl;
	
	@Override
	public User getCreator() {
		return creator;
	}

	@Override
	public void setCreator(User creator) {
		this.creator = creator;
		creatorIds = getCreatorPath(creator);
	}

	@Override
	public String getCreatorIds() {
		return creatorIds;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		return content;
	}


	public void setLang(String lang) {
		this.lang = lang;
	}

	public String getLang() {
		return lang;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setUpdatedAt(Date updatedAt) {
		this.updatedAt = updatedAt;
	}

	public Date getUpdatedAt() {
		return updatedAt;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getType() {
		return type;
	}

	public void setDspType(String dspType) {
		this.dspType = dspType;
	}

	public String getDspType() {
		return dspType;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getFlag() {
		return flag;
	}

	public void setOpenSource(boolean openSource) {
		this.openSource = openSource;
	}

	public boolean isOpenSource() {
		return openSource;
	}

	public void setTplUsage(String tplUsage) {
		this.tplUsage = tplUsage;
	}

	public String getTplUsage() {
		return tplUsage;
	}

	public void setOutScript(String outScript) {
		this.outScript = outScript;
	}

	public String getOutScript() {
		return outScript;
	}

	public void setPreviewUrl(String previewUrl) {
		this.previewUrl = previewUrl;
	}

	public String getPreviewUrl() {
		return previewUrl;
	}

	@Override
	public String[] getJsonExcludes() {
		// TODO Auto-generated method stub
		return null;
	}


}
